﻿using DevExpress.XtraGrid.Demos.util;
using DXApplicationTangche.UC.抖音出入库.Service;
using DXApplicationTangche.UC.金蝶.dto;
using DXApplicationTangche.UC.金蝶.金蝶收款单.Service;
using DXApplicationTangche.UC.金蝶.金蝶退款单.Service;
using DXApplicationTangche.UC.面料审核.Service;
using DXApplicationTangche.Utils;
using Kingdee.BOS.WebApi.Client;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static DXApplicationTangche.UC.金蝶.dto.MaterialResultDTO;

namespace DXApplicationTangche.UC.抖音出入库
{
    public partial class Form抖音订单下达收款单 : DevExpress.XtraBars.Ribbon.RibbonForm
    {
        private List<DouyinFinancialSimpleDTO> douyinFinancialSimpleDTOs { get; set; } = new List<DouyinFinancialSimpleDTO>();
        public Form抖音订单下达收款单()
        {
            InitializeComponent();
            this.gridControl收款金额.DataSource = this.douyinFinancialSimpleDTOs;
        }
        private void 刷新()
        {
            //弹框加载提示
            this.splashScreenManager.ShowWaitForm();
            this.splashScreenManager.SetWaitFormCaption("请稍后,正在加载中....");     // 标题
            this.splashScreenManager.SetWaitFormDescription("上传成功，正在保存金蝶收款单.....");
            //加载数据
            DouyinFinancialQueryFacadeDTO douyinFinancialQueryFacadeDTO = 抖音订单Service.getDouyinFinancialSimpleAll("销售收款");
            this.douyinFinancialSimpleDTOs = douyinFinancialQueryFacadeDTO.douyinFinancialSimpleDTOS;
            //删除当日订单
            foreach(DouyinFinancialSimpleDTO douyinFinancialSimpleDTO in this.douyinFinancialSimpleDTOs)
            {
                if (douyinFinancialSimpleDTO.dateStr.Equals(DateTime.Now.ToString("yyyy-MM-dd")))
                {
                    this.douyinFinancialSimpleDTOs.Remove(douyinFinancialSimpleDTO);
                }
            }
            String xx = FunctionHelper.JsonSerialization(douyinFinancialQueryFacadeDTO);
            String xxx = FunctionHelper.JsonSerialization(this.douyinFinancialSimpleDTOs);
            //下达采购订单
            //this.保存收款单(this.douyinFinancialSimpleDTOs);
            this.gridControl收款金额.DataSource = this.douyinFinancialSimpleDTOs;
            List<ViewDouyinOrderSaleFundDTO> viewDouyinOrderSaleFundDTOs显示 = new List<ViewDouyinOrderSaleFundDTO>();
            foreach(ViewDouyinOrderSaleFundDTO viewDouyinOrderSaleFundDTO in douyinFinancialQueryFacadeDTO.viewDouyinOrderSaleFundDTOList)
            {
                if (!viewDouyinOrderSaleFundDTO.dateStamp.Equals(DateTime.Now.ToString("yyyy-MM-dd").Replace("-", "")))
                {
                    viewDouyinOrderSaleFundDTOs显示.Add(viewDouyinOrderSaleFundDTO);
                }
            }
            this.gridControl订单明细.DataSource = viewDouyinOrderSaleFundDTOs显示;
            this.gridView收款金额.RefreshData();
            this.gridView订单明细.RefreshData();
            this.gridView订单明细.ExpandAllGroups();
            //MessageBox.Show("金蝶收款单保存成功");
            this.splashScreenManager.CloseWaitForm();
        }
        private void 刷新退款单()
        {
            //弹框加载提示
            this.splashScreenManager.ShowWaitForm();
            this.splashScreenManager.SetWaitFormCaption("请稍后,正在加载中....");     // 标题
            this.splashScreenManager.SetWaitFormDescription("上传成功，正在保存金蝶收款单.....");
            //加载数据
            DouyinFinancialQueryFacadeDTO douyinFinancialQueryFacadeDTO = 抖音订单Service.getDouyinFinancialSimpleAll("销售退款");
            //DouyinFinancialQueryFacadeDTO douyinFinancialQueryFacadeDTO = 抖音订单Service.getDouyinFinancialSimple("销售退款","2021-07-15");
            this.douyinFinancialSimpleDTOs = douyinFinancialQueryFacadeDTO.douyinFinancialSimpleDTOS;
            //删除当日订单
            foreach (DouyinFinancialSimpleDTO douyinFinancialSimpleDTO in this.douyinFinancialSimpleDTOs)
            {
                if (douyinFinancialSimpleDTO.dateStr.Equals(DateTime.Now.ToString("yyyy-MM-dd")))
                {
                    this.douyinFinancialSimpleDTOs.Remove(douyinFinancialSimpleDTO);
                }
            }
            String xx = FunctionHelper.JsonSerialization(douyinFinancialQueryFacadeDTO);
            String xxx = FunctionHelper.JsonSerialization(this.douyinFinancialSimpleDTOs);
            //下达退款单
            //this.保存退款单(this.douyinFinancialSimpleDTOs);
            this.gridControl收款金额.DataSource = this.douyinFinancialSimpleDTOs;
            List<ViewDouyinOrderSaleFundDTO> viewDouyinOrderSaleFundDTOs显示 = new List<ViewDouyinOrderSaleFundDTO>();
            foreach (ViewDouyinOrderSaleFundDTO viewDouyinOrderSaleFundDTO in douyinFinancialQueryFacadeDTO.viewDouyinOrderSaleFundDTOList)
            {
                if (!viewDouyinOrderSaleFundDTO.dateStamp.Equals(DateTime.Now.ToString("yyyy-MM-dd").Replace("-", "")))
                {
                    viewDouyinOrderSaleFundDTOs显示.Add(viewDouyinOrderSaleFundDTO);
                }
            }
            this.gridControl订单明细.DataSource = viewDouyinOrderSaleFundDTOs显示; this.gridView收款金额.RefreshData();
            this.gridView订单明细.RefreshData();
            this.gridView订单明细.ExpandAllGroups();
            //MessageBox.Show("金蝶退款单保存成功");
            this.splashScreenManager.CloseWaitForm();
        }

        private void 金蝶保存收款单ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.douyinFinancialSimpleDTOs.Count() < 1)
            {
                MessageBox.Show("暂无订单数据，请勿下达金蝶收款单");
                return;
            }
            //this.保存收款单(this.douyinFinancialSimpleDTOs);
        }
        //private void 保存收款单(List<DouyinFinancialSimpleDTO> douyinFinancialSimpleDTOs)
        //{
        //    K3CloudApiClient client = new K3CloudApiClient("http://192.168.3.251/k3cloud/");
        //    var loginResult = client.ValidateLogin("6128407938e2f2", "Administrator", "Sshirt@1234", 2052);
        //    var resultType = JObject.Parse(loginResult)["LoginResultType"].Value<int>();
        //    //登录结果类型等于1，代表登录成功
        //    if (resultType == 1)
        //    {
        //        foreach(DouyinFinancialSimpleDTO douyinFinancialSimpleDTO in douyinFinancialSimpleDTOs)
        //        {
        //            List<FRECEIVEBILLENTRYItem> fRECEIVEBILLENTRYItems = new List<FRECEIVEBILLENTRYItem>();
        //            double payCount = douyinFinancialSimpleDTO.payable;
        //            String FDATE = douyinFinancialSimpleDTO.dateStr;
        //            fRECEIVEBILLENTRYItems.Add(new FRECEIVEBILLENTRYItem("JSFS04_SYS", "SFKYT002", payCount, "411907698210333"));
        //            String result = client.Save("AR_RECEIVEBILL", FunctionHelper.JsonSerialization(new ReceieveBilRoot("SKDLX01_SYS", "BD_Customer", "CUST0004", "BD_Customer", "CUST0004", FDATE, fRECEIVEBILLENTRYItems)));
        //            KingDeeAddResultRoot materialResultRoot = FunctionHelper.JsonDeserialization<KingDeeAddResultRoot>(result);
        //            String Submitresult = client.Submit("AR_RECEIVEBILL", FunctionHelper.JsonSerialization(new 金蝶.dto.MaterialSubmitDTO(materialResultRoot.Result.Number, materialResultRoot.Result.Id.ToString())));
        //            String SupplierVerifyresult = client.Audit("AR_RECEIVEBILL", FunctionHelper.JsonSerialization(new 金蝶.dto.MaterialVerifyDTO(materialResultRoot.Result.Number, materialResultRoot.Result.Id.ToString())));
        //        }
        //        ////String result = client.View("AR_RECEIVEBILL", "{\"Number\":\"SKD00000010\",\"Id\":\"100010\"}");
        //        //String result = client.Save("AR_RECEIVEBILL", FunctionHelper.JsonSerialization(new ReceieveBilRoot("SKDLX01_SYS", "BD_Customer", "CUST0004", "BD_Customer", "CUST0004", FDATE, fRECEIVEBILLENTRYItems)));

        //    }
        //    MessageBox.Show("收款单保存完成");
        //}
        //private void 保存退款单(List<DouyinFinancialSimpleDTO> douyinFinancialSimpleDTOs)
        //{
        //    K3CloudApiClient client = new K3CloudApiClient("http://192.168.3.251/k3cloud/");
        //    var loginResult = client.ValidateLogin("6128407938e2f2", "Administrator", "Sshirt@1234", 2052);
        //    var resultType = JObject.Parse(loginResult)["LoginResultType"].Value<int>();
        //    //登录结果类型等于1，代表登录成功
        //    if (resultType == 1)
        //    {
        //        foreach (DouyinFinancialSimpleDTO douyinFinancialSimpleDTO in douyinFinancialSimpleDTOs)
        //        {
        //            List<FREFUNDBILLENTRYItem> fREFUNDBILLENTRYItems = new List<FREFUNDBILLENTRYItem>();
        //            double payCount = douyinFinancialSimpleDTO.payable;
        //            String FDATE = douyinFinancialSimpleDTO.dateStr;
        //            fREFUNDBILLENTRYItems.Add(new FREFUNDBILLENTRYItem("JSFS04_SYS", "SFKYT002", payCount, "411907698210333"));
        //            String result = client.Save("AR_REFUNDBILL", FunctionHelper.JsonSerialization(new RefoundBillRoot("SKTKDLX01_SYS", "BD_Customer", "CUST0004", "BD_Customer", "CUST0004", FDATE, fREFUNDBILLENTRYItems)));
        //            KingDeeAddResultRoot materialResultRoot = FunctionHelper.JsonDeserialization<KingDeeAddResultRoot>(result);
        //            String Submitresult = client.Submit("AR_REFUNDBILL", FunctionHelper.JsonSerialization(new 金蝶.dto.MaterialSubmitDTO(materialResultRoot.Result.Number, materialResultRoot.Result.Id.ToString())));
        //            String SupplierVerifyresult = client.Audit("AR_REFUNDBILL", FunctionHelper.JsonSerialization(new 金蝶.dto.MaterialVerifyDTO(materialResultRoot.Result.Number, materialResultRoot.Result.Id.ToString())));
        //        }
        //        ////String result = client.View("AR_RECEIVEBILL", "{\"Number\":\"SKD00000010\",\"Id\":\"100010\"}");
        //        //String result = client.Save("AR_RECEIVEBILL", FunctionHelper.JsonSerialization(new ReceieveBilRoot("SKDLX01_SYS", "BD_Customer", "CUST0004", "BD_Customer", "CUST0004", FDATE, fRECEIVEBILLENTRYItems)));

        //    }
        //    MessageBox.Show("退款单保存完成");
        //}

        private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "csv|*.csv";
            ofd.Title = "请选择csv文件";
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                this.splashScreenManager.ShowWaitForm();
                this.splashScreenManager.SetWaitFormCaption("请稍后,正在加载中....");     // 标题
                this.splashScreenManager.SetWaitFormDescription("正在上传抖音订单.....");     // 信息
                //  csv导入
                面料审核Service.saveDouyinOrder("SSHIRT", "11", ofd.FileName);
                this.splashScreenManager.CloseWaitForm();
                this.刷新();
            }
        }

        private void barButtonItem2_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            OpenFileDialog openFile = new OpenFileDialog();
            openFile.Filter = MIMEConst.EXCEL_MIME;
            openFile.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            openFile.Multiselect = false;
            if (openFile.ShowDialog() == DialogResult.OK)
            {
                ExcelAndPath excelAndPath = ExcelUtil.getExcelDataAndPath(openFile.FileName);
                if (excelAndPath == null)
                {
                    return;
                }
                DataSet result = excelAndPath.DataSet;
                if (result == null)
                {
                    return;
                }
                DataTable dataTable = result.Tables[0];
                //获取数据
                List<DouyinOrderReturnDTO> douyinOrderReturnDTOs = new List<DouyinOrderReturnDTO>();
                for (int i = 1; i < dataTable.Rows.Count; i++)
                {
                    try
                    {
                        if (String.IsNullOrEmpty(dataTable.Rows[i]["Column0"].ToString()))
                        {
                            break;
                        }
                    }
                    catch (Exception)
                    {
                        break;
                    }
                    #region
                    douyinOrderReturnDTOs.Add(
                        new DouyinOrderReturnDTO(
                         dataTable.Rows[i]["Column0"].ToString(),    //售后单号
                         dataTable.Rows[i]["Column1"].ToString(),  //订单号
                         dataTable.Rows[i]["Column2"].ToString(),  //商品单号
                         dataTable.Rows[i]["Column3"].ToString(), //商品名称
                         dataTable.Rows[i]["Column4"].ToString(),   //商品ID
                         dataTable.Rows[i]["Column5"].ToString(), //应付金额（元）
                         dataTable.Rows[i]["Column6"].ToString(),   //商品运费（元）
                         dataTable.Rows[i]["Column7"].ToString(),//支付优惠（元）
                         dataTable.Rows[i]["Column8"].ToString(),   //商品税费（元）
                         dataTable.Rows[i]["Column9"].ToString(),  //商品发货状态
                         dataTable.Rows[i]["Column10"].ToString(),   //售后类型
                         dataTable.Rows[i]["Column11"].ToString(), //退商品金额（元）
                         dataTable.Rows[i]["Column12"].ToString(), //退运费金额（元）
                         dataTable.Rows[i]["Column13"].ToString(), //退支付优惠（元）
                         dataTable.Rows[i]["Column14"].ToString(),   //退税费金额（元）
                         dataTable.Rows[i]["Column15"].ToString(), //售后状态
                         dataTable.Rows[i]["Column16"].ToString(),    //售后申请时间
                         dataTable.Rows[i]["Column17"].ToString(),  //退款方式
                         dataTable.Rows[i]["Column18"].ToString(),  //是否已上传退款凭证
                         dataTable.Rows[i]["Column19"].ToString(), //售后原因
                         dataTable.Rows[i]["Column20"].ToString(),  //退货物流单号
                         dataTable.Rows[i]["Column21"].ToString(), //退货异常
                         dataTable.Rows[i]["Column22"].ToString(), //退货发货时间
                         dataTable.Rows[i]["Column23"].ToString(),  //退货物流公司
                         dataTable.Rows[i]["Column24"].ToString(),    //自动处理截止时间
                         dataTable.Rows[i]["Column25"].ToString(),  //同意售后申请时间
                         dataTable.Rows[i]["Column26"].ToString(),    //退款成功时间
                         dataTable.Rows[i]["Column27"].ToString(),    //售后关闭时间
                         dataTable.Rows[i]["Column28"].ToString(),  //商家退货地址
                         dataTable.Rows[i]["Column29"].ToString(),    //商家退货联系人姓名
                         dataTable.Rows[i]["Column30"].ToString(),  //商家退货联系人电话
                         dataTable.Rows[i]["Column31"].ToString(),  //客服仲裁
                         dataTable.Rows[i]["Column32"].ToString(),  //纠纷责任方
                         dataTable.Rows[i]["Column33"].ToString(),    //是否极速退款
                         dataTable.Rows[i]["Column34"].ToString(), //正向物流状态
                         dataTable.Rows[i]["Column35"].ToString()   //是否拒签后退款
                        )
                    );
                    #endregion
                }
                抖音订单Service.saveDouyinOrderReturn(douyinOrderReturnDTOs);
                this.刷新退款单();
            }
        }

        private void barButtonItem3_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            //ofd.Filter = "csv|*.csv";
            ofd.Title = "请选择csv文件";
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                //  csv导入
                String re = 面料审核Service.saveDouyinOrder("SSHIRT", "11", ofd.FileName);
                MessageBox.Show("订单csv上传成功");
            }
        }

        private void barButtonItem4_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            OpenFileDialog openFile = new OpenFileDialog();
            openFile.Filter = MIMEConst.EXCEL_MIME;
            openFile.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            openFile.Multiselect = false;
            if (openFile.ShowDialog() == DialogResult.OK)
            {
                ExcelAndPath excelAndPath = ExcelUtil.getExcelDataAndPath(openFile.FileName);
                if (excelAndPath == null)
                {
                    return;
                }
                DataSet result = excelAndPath.DataSet;
                if (result == null)
                {
                    return;
                }
                DataTable dataTable = result.Tables[0];
                //获取数据
                List<DouyinOrderReturnDTO> douyinOrderReturnDTOs = new List<DouyinOrderReturnDTO>();
                for (int i = 1; i < dataTable.Rows.Count; i++)
                {
                    try
                    {
                        if (String.IsNullOrEmpty(dataTable.Rows[i]["Column0"].ToString()))
                        {
                            break;
                        }
                    }
                    catch (Exception)
                    {
                        break;
                    }
                    #region
                    douyinOrderReturnDTOs.Add(
                        new DouyinOrderReturnDTO(
                         dataTable.Rows[i]["Column0"].ToString(),    //售后单号
                         dataTable.Rows[i]["Column1"].ToString(),  //订单号
                         dataTable.Rows[i]["Column2"].ToString(),  //商品单号
                         dataTable.Rows[i]["Column3"].ToString(), //商品名称
                         dataTable.Rows[i]["Column4"].ToString(),   //商品ID
                         dataTable.Rows[i]["Column5"].ToString(), //应付金额（元）
                         dataTable.Rows[i]["Column6"].ToString(),   //商品运费（元）
                         dataTable.Rows[i]["Column7"].ToString(),//支付优惠（元）
                         dataTable.Rows[i]["Column8"].ToString(),   //商品税费（元）
                         dataTable.Rows[i]["Column9"].ToString(),  //商品发货状态
                         dataTable.Rows[i]["Column10"].ToString(),   //售后类型
                         dataTable.Rows[i]["Column11"].ToString(), //退商品金额（元）
                         dataTable.Rows[i]["Column12"].ToString(), //退运费金额（元）
                         dataTable.Rows[i]["Column13"].ToString(), //退支付优惠（元）
                         dataTable.Rows[i]["Column14"].ToString(),   //退税费金额（元）
                         dataTable.Rows[i]["Column15"].ToString(), //售后状态
                         dataTable.Rows[i]["Column16"].ToString(),    //售后申请时间
                         dataTable.Rows[i]["Column17"].ToString(),  //退款方式
                         dataTable.Rows[i]["Column18"].ToString(),  //是否已上传退款凭证
                         dataTable.Rows[i]["Column19"].ToString(), //售后原因
                         dataTable.Rows[i]["Column20"].ToString(),  //退货物流单号
                         dataTable.Rows[i]["Column21"].ToString(), //退货异常
                         dataTable.Rows[i]["Column22"].ToString(), //退货发货时间
                         dataTable.Rows[i]["Column23"].ToString(),  //退货物流公司
                         dataTable.Rows[i]["Column24"].ToString(),    //自动处理截止时间
                         dataTable.Rows[i]["Column25"].ToString(),  //同意售后申请时间
                         dataTable.Rows[i]["Column26"].ToString(),    //退款成功时间
                         dataTable.Rows[i]["Column27"].ToString(),    //售后关闭时间
                         dataTable.Rows[i]["Column28"].ToString(),  //商家退货地址
                         dataTable.Rows[i]["Column29"].ToString(),    //商家退货联系人姓名
                         dataTable.Rows[i]["Column30"].ToString(),  //商家退货联系人电话
                         dataTable.Rows[i]["Column31"].ToString(),  //客服仲裁
                         dataTable.Rows[i]["Column32"].ToString(),  //纠纷责任方
                         dataTable.Rows[i]["Column33"].ToString(),    //是否极速退款
                         dataTable.Rows[i]["Column34"].ToString(), //正向物流状态
                         dataTable.Rows[i]["Column35"].ToString()   //是否拒签后退款
                        )
                    );
                    #endregion
                }
                抖音订单Service.saveDouyinOrderReturn(douyinOrderReturnDTOs);
            }
            MessageBox.Show("售后单导入成功");
        }
    }
}
